package com.hmyuan.controller.front;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hmyuan.domain.Files;
import com.hmyuan.service.FilesService;
import com.hmyuan.util.images.ImgForText;

/**
 * 文件controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/files")
public class FrontFilesController {

	@Resource
	FilesService filesService;
	/**
	 * 根据图片id显示图片
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/filesShowById")
	public String filesShowById(HttpServletRequest request,HttpServletResponse response,Model model,Long id) throws IOException{
        OutputStream os = null;  
        byte[] filebyte = filesService.filesShowById(id);
        if(filebyte==null){
            return null;  
        }
        try {  
            os = response.getOutputStream(); 
            os.write(filebyte);
        } catch (Exception e) {  
            e.printStackTrace();  
            os.close();  
        }  
        return null;  
	}
	/**
	 * 根据图片路径显示图片
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/filesShow/{path1}/{path2}/{fileName:.+}")
	public String filesShow(HttpServletRequest request,HttpServletResponse response,Model model,
			@PathVariable String path1,
			@PathVariable String path2,
			@PathVariable String fileName) throws IOException{
		OutputStream os = null;  
		byte[] filebyte = filesService.filesShow(path1+"/"+path2+"/"+fileName);
		if(filebyte==null){
			return null;  
		}
		try {  
			os = response.getOutputStream(); 
			os.write(filebyte);
		} catch (Exception e) {  
			e.printStackTrace();  
			os.close();  
		}  
		return null;  
	}
	
}
